$(document).ready(function () {
    getTreeData()
});

function getTreeData() {
    var type = $('#type').val();
    $.ajax({
        type: "GET",
        url: "/yw/appraisal/tree?type=" + type,
        success: function (tree) {
            loadTree(tree);
        }
    });
}

function loadTree(tree) {
    $('#appraisalTree').jstree({
        'core': {
            'data': tree
        },
        "plugins": ["search"]
        // "plugins": ["checkbox", "search"]
    });
    $('#appraisalTree').jstree().open_all();
}

function search() {
    $("#appraisalTree").jstree(true).search($("#searchName").val());
}

$('#appraisalTree').on("changed.jstree", function (e, data) {
    var tree = $('#appraisalTree').jstree(true);
    if (tree.is_leaf(data.node)) {
        var paId = tree.get_parent(data.node);
        var pa = tree.get_node(paId, true);
        var paName = tree.get_text(pa);
        console.log(paId);
        console.log(paName);
        if (paId.slice(0, 4) === 'type') {
            if (data.node.id !== -1) {
                parent.loadAppraisalOne(data.node.id, data.node.text, data.node.text);
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            }
        } else {
            if (data.node.id !== -1) {
                parent.loadAppraisalOne(paId, paName, paName + "：" + data.node.text);
                parent.loadAppraisalTwo(data.node.id, data.node.text);
                parent.layer.close(parent.layer.getFrameIndex(window.name));// 获取窗口索引
            }
        }
    } else {
        layer.msg("当前指标不可被选择")
    }
});

